<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>寻找两个正序数组的中位数</title>
  </head>
  <body>
    <script>
      // var a = 123;
      // b = (a / 2).toFixed(5);
      // console.log(b);
      //----------方法一-----------------
      //1.通过concatenate()函数合并两数组为arr
      //2.arr.sort(function(a,b){return a-b});(由小到大排序)
      //3.通过arr.length/2 if(arr.length为单数)：arr[Math.floor(arr.length/2)]
      //-------------------if(arr.length为双数)：(arr[arr.length/2] + arr[arr.length/2 - 1])/2
      //(注意长度除以二获得的中位数位数要减一，因为数组下标从零开始)
      //4.通过number.toFixed(x);给number保留x位小数
      var findMedianSortedArrays = function (n1, n2) {
        var arr = n1.concat(n2);
        arr.sort(function (a, b) {
          return a - b;
        });
        if (arr.length % 2 == 0) {
          return ((arr[arr.length / 2] + arr[arr.length / 2 - 1]) / 2).toFixed(
            5
          );
        } else {
          return arr[Math.floor(arr.length / 2)].toFixed(5);
        }
      };
      console.log(findMedianSortedArrays([1, 2], [3, 4]));
      //--------------------方法二---------------------
      function findMed(n1, n2) {
        var arrLength = n1.length + n2.length;
        if (n1.length > n2.length) {
          var n3 = n1;
          n1 = n2;
          n2 = n3;
        }
        var n2Length = n2.length;
        var x = 0;
        var y = 0;
        var m = 0,
          n = 0;
        var flag1 = 0;
        var flag2 = 0;
        if (arrLength % 2 == 1) {
          //数组总长度为个数时
          for (let i = 0; i < arrLength / 2; i++) {
            flag1 = 0;
            flag2 = 0;
            if (n1[x] < n2[y]) {
              m = x;
              x++;
              flag1 = 1;
            } else {
              n = y;
              y++;
              flag2 = 1;
            }
          }
          if (flag1) {
            return n1[m].toFixed(5);
          } else {
            return n2[n].toFixed(5);
          }
        } else {
          var j = 0,
            k = 0;
          for (let i = 0; i < arrLength / 2 + 1; i++) {
            flag1 = 0;
            flag2 = 0;
            if (n1[x] < n2[y]) {
              m = x;
              if (i == arrLength / 2 - 1) {
                j = x;
              }
              x++;
              flag1 = 1;
            } else if (y < n2Length) {
              n = y;
              if (i == arrLength / 2 - 1) {
                k = y;
              }
              y++;
              flag2 = 1;
            } else {
              m = x;
              if (i == arrLength / 2 - 1) {
                j = x;
              }
              x++;
              flag1 = 1;
            }
          }
          if (flag1) {
            return j == 0
              ? ((n1[m] + n2[k]) / 2).toFixed(5)
              : ((n1[m] + n1[j]) / 2).toFixed(5);
          } else {
            return j == 0
              ? ((n2[n] + n2[k]) / 2).toFixed(5)
              : ((n2[n] + n1[j]) / 2).toFixed(5);
          }
        }
      }
      console.log(findMed([1, 2], [-1, 3]));
    </script>
  </body>
</html>
